package com.yanchangapp.ui.presenter;

import android.util.Log;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.RejectRecordRequest;
import com.yanchangapp.model.request.UpRecordRequest;
import com.yanchangapp.model.response.RecordDetailResponse;
import com.yanchangapp.model.response.UpdateRecordResponse;
import com.yanchangapp.ui.view.ApproveWaitDetailAtView;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-29.
 * description:审批详情p层
 */
public class ApproveWaitDetailAtPresenter extends BasePresenter<ApproveWaitDetailAtView> {

    public ApproveWaitDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryRecordDetail(int itemID){
        HeadApiRetrofit.getInstance().queryRecordDetailByItemId(itemID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<RecordDetailResponse>>() {
                    @Override
                    public void onNext(BaseResponse<RecordDetailResponse> recordDetailResponseBaseResponse) {
                        if (null != getView()){
                            if (AppConstant.REQUEST_SUCCESS == recordDetailResponseBaseResponse.getCode()){
                                getView().queryDetailSuccess(recordDetailResponseBaseResponse.getData());
                            }else {
                                getView().queryDetailFailure(recordDetailResponseBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        Log.e("错误", "onError: 解析出错" );
                        if (null != getView()){
                            getView().queryDetailFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void submitRecord(int id,String supplierId,int purchaseNum){
        if (null != getView()){
            getView().showLoadingDialog();
        }
        UpRecordRequest request = new UpRecordRequest(id,supplierId,purchaseNum);
        HeadApiRetrofit.getInstance().updateRecordProduct(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UpdateRecordResponse>() {
                    @Override
                    public void onNext(UpdateRecordResponse baseResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().editRecordSuccess(baseResponse.getData());
                            }else {
                                getView().editRecordFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().editRecordFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void rejectRecord(int id,String reason){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        RejectRecordRequest rejectRecordRequest = new RejectRecordRequest(id,reason);
        HeadApiRetrofit.getInstance().rejectRecord(rejectRecordRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().rejectRecordSuccess();
                            }else {
                                getView().rejectRecordFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().rejectRecordFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void passRecord(int id){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().passRecord(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().passRecordSuccess();
                            }else {
                                getView().passRecordFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().passRecordFailure(exception.getErrorInfo());
                        }
                    }
                });
    }
}
